<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 认证列表管理
 *
 * @icon fa fa-circle-o
 */
class Approve extends Backend
{
    
    /**
     * Approve模型对象
     * @var \app\admin\model\user\Approve
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\Approve;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function audit($ids)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $data['status'] = $params['status'];
                    $result = $row->allowField(true)->save($data);
                    if ($result !== false)
                    {
                        $user = new \app\admin\model\user\User;
                        if ($params['status'] == 1 ) {
                            $res['approve_status'] = 2;
                            $res['approve_title']  = $params['approve_title'];
                        } else if ($params['status'] == 2) {
                            $res['approve_status'] = 0;
                        }
                        $user->where('id', $row['user_id'])->update($res);
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
}
